<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/24
 * Time: 11:27
 */

namespace app\admin\controller;

use think\Controller;
use app\api\controller\KbxToken as KbxToken;
use think\Cookie;
use think\Db;

class Exchange extends Controller
{

    public function index(){
        $exchange=  db('exchange')->select();
        foreach ($exchange as $k=>$v){
            $admin=Db::name('admin')->where(['id'=>$v['admin_id']])->find();
            $exchange[$k]['admin']=$admin['nickname'];
        }

        $this->assign('exchange',$exchange);
        return $this->fetch();
    }


    public function edit(){
        if ($this->request->isPost()){
            $adminid = Cookie::get('admin');

            $params=request()->post();
            $params1['updatetime']=time();
            $params1['exchange_rate']=$params['exchange_rate'];
            $params1['admin_id']=$adminid;
            $data=Db::name('exchange')->where(['id'=>$params['id']])->update(
                $params1
            );
            if (!$data){
                return $this->error('修改失败','','0');
            }
            return  $this->success('修改成功','','1');
        }
        $ids=$this->request->param('uid');
        $info=Db::name('exchange')->where(['id'=>$ids])->find();
        $this->assign('info',$info);
        return $this->fetch();
    }


    //获取汇率   调用订单系统接口
    public function get_rate(){
        $KbxToken = new KbxToken();
        $url=$KbxToken->url.'/client/getExchangeRate';
        $token='Bearer '.$KbxToken->kbxToken();
        $rate=$KbxToken->get_data($url,$token);
        $rate=$KbxToken->object_array($rate);

        return $rate['rates'];
    }


}